﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSE.ProcessManager.Common.Gateway
{
    public class InstanceListenerCallback : IListenerServiceCallback
    {
        #region IListenerServiceCallback Members

        public event ServiceEvents.MessageReceived OnReceiveMessage;

        #endregion


        #region IListenerServiceCallback Members

        public void SendMessageCallBack(System.ServiceModel.Channels.Message message)
        {
            BaseMessage baseMessage = new BaseMessage();
            ProcessMessageHeader header = message.Headers.GetHeader<ProcessMessageHeader>(ProcessMessageHeader.Name, ProcessMessageHeader.NameSpace);
            baseMessage.MessageId = header.MessageId;
            baseMessage.Operation = header.Operation;
            baseMessage.ProcessId = header.ProcessId;
            baseMessage.From = header.From;
            baseMessage.To = header.To;

            baseMessage.Original = message.CreateBufferedCopy(int.MaxValue);

            if (OnReceiveMessage != null)
            {
                OnReceiveMessage(baseMessage);
            }
        }

        public IAsyncResult BeginSendMessageCallBack(System.ServiceModel.Channels.Message message, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndSendMessageCallBack(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
